package com.gmail.dmitryustimov.ms.java7.visitor.mod_actionmap.example.example1;

/**
 * Author: Dmitry Ustimov
 * Date: 5/10/13
 * Time: 4:27 PM
 */
public class Demo {

    public static void main(String[] args) {
        new Demo().makeDemo();
    }

    private void makeDemo() {
        Element elementA = new ElementA("Characteristic A");
        Element elementB = new ElementB();

        Visitor<Element> printVisitor = new Visitor<>();
        printVisitor.addVisitAction(elementA.getClass(), this.new PrintA());
        printVisitor.addVisitAction(elementB.getClass(), this.new PrintB());

        printVisitor.visit(elementA);
        printVisitor.visit(elementB);


        Visitor<Element> doVisitor = new Visitor<>();
        doVisitor.addVisitAction(elementA.getClass(), this.new DoA());
        doVisitor.addVisitAction(elementB.getClass(), this.new DoB());

        doVisitor.visit(elementA);
        doVisitor.visit(elementB);
    }

    public class PrintA implements VisitAction<ElementA> {
        @Override
        public void visit(ElementA visitable) {
            System.out.println("Print an element A of characteristic: " + visitable.characteristic);
        }
    }

    public class PrintB implements VisitAction<ElementB> {
        @Override
        public void visit(ElementB visitable) {
            System.out.println("Print an element B");
        }
    }

    public class DoA implements VisitAction<ElementA> {
        @Override
        public void visit(ElementA visitable) {
            System.out.println("Do something with an element A of characteristic: " + visitable.characteristic);
        }
    }

    public class DoB implements VisitAction<ElementB> {
        @Override
        public void visit(ElementB visitable) {
            System.out.println("Do something with an element B");
        }
    }
}
